import json
from sqlalchemy import Column, Date, String, ForeignKey, BigInteger, Uuid, LargeBinary, JSON
from models import BaseModels


# 设备表
class Facility(BaseModels):
    __tablename__ = 'facility'
    id = Column(BigInteger, autoincrement=True, primary_key=True, unique=True, nullable=False)
    workplace = Column(String(128), nullable=True, comment="设备存放地址")
    serials = Column(String(64), nullable=False, unique=True, comment="设备编号NO")
    name = Column(String(64), nullable=False, comment="设备名字")
    station = Column(String(64), nullable=False, comment="工站名称")
    user_id = Column(BigInteger, ForeignKey("users.id"), comment="哪个用户添加的")
    time = Column(Date, nullable=False, comment="设备添购日期")
    expired = Column(Date, nullable=False, comment="设备有效截至日期")
    alarms = Column(LargeBinary, nullable=True, comment="设备报警数据结构题")
    # alarms = Column(JSON, nullable=True, comment="设备报警数据结构题")
    uuid4 = Column(Uuid, nullable=True, unique=False, comment="缓存UUID4编号")

    def __repr__(self):
        return json.dumps({
            "facility_id": self.id,
            "workplace": self.workplace,
            "serials": self.serials,
            "name": self.name,
            "station_id": self.station,
            "user_id": self.user_id,
            "time": self.time.strftime("%Y-%m-%d"),
            "expired": self.expired.strftime("%Y-%m-%d")
        })
